//
//  ResultInfo.m
//  DebugDemo
//
//  Created by XFB on 2018/11/23.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "ResultInfo.h"

static ResultInfo *_resultInfo = nil;

@implementation ResultInfo

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _resultInfo = [[ResultInfo alloc] init];
    });
    return _resultInfo;
}

+ (ResultInfo *)convertResponseToResultInfo:(id)response
{
    ResultInfo *resultInfo = [ResultInfo shareInstance];
    resultInfo.resultCode = 0;
    if (response == nil) {
        return nil;
    }
    
    NSError *error;
    NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    /// 将字符串的形式转为NSData类型
    NSData *data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    
    /// NSData -> NSDictionary
    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    if (responseDict == nil) {
        return nil;
    }
    
    /// 返回状态码
    NSInteger resultCode = [[responseDict objectForKey:@"msg_type"] integerValue];
    resultInfo.resultCode = resultCode;
    /// 返回状态码信息
    NSString *codeMessage = responseDict[@"msg"];
    resultInfo.resultMessage = codeMessage;
    /// 返回数据
    resultInfo.resultContent = responseDict[@"data"];
    return resultInfo;
}

@end
